﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

/// <summary>
///Forum 的摘要说明
/// </summary>
public class CForum
{
    public int m_fid;
    public string m_name;
    public string m_description;
    public List<Topic> topics;
    public int topicCount;
    public int isExist;
    public int topicTodayCount;
    public int isVisible;
    public int searchCount;
    public static int TOPICKTITLELEN = 30;


	public CForum(int fid)
	{
        m_fid = fid;
        topics = new List<Topic>();
        GetData gd = new GetData();
        forums f = gd.GetForum(fid);
        if (f == null)
        {
            isExist = 0;
        }
        else
        {
            isExist = 1;
            m_description = f.description;
            m_name = f.name;
        }
	}

    public CForum()
    {
        topics = new List<Topic>();
    }

    public CForum(forums f)
    {
        this.m_fid = f.fid;
        this.m_name = f.name;
        this.m_description = f.description;
    }
    
    public void GetDescription()
    {
        GetData gd = new GetData();
        forums f;
        f = gd.GetForum(m_fid);
        m_description = f.description;
        m_name = f.name;
    }

    public void CutDescription()
    {
        if (m_description.Length > 19)
        {
            m_description = m_description.Substring(0, 19);
            m_description += "...";
        }
    }

    public void getTopicCount(int type)
    {
        GetData gd = new GetData();
        if (type == 0)//只获取未删除
            topicCount = gd.GetThreadsCount(m_fid);
        else if (type == 1)//获取全部
            topicCount = gd.GetThreadsAllCount(m_fid);
    }

    public void GetTodayTopic()
    {
        GetData gd = new GetData();
        topicTodayCount = gd.GetTodayThreadCount(m_fid, DateTime.Today);
    }

    public void getTopic()
    {
        GetData gd = new GetData();
        List<threads> to = gd.GetThreads(m_fid);
        if (to == null)
            return;
        foreach (threads t in to)
        {
            Topic tt = new Topic(t);
            tt.CutTitle(TOPICKTITLELEN);
            topics.Add(tt);
        }
    }

    public void getTopic(int pageSize,int pageIndex,int type)
    { 
        GetData gd = new GetData();
        List<threads> to=new List<threads>();
        if(type==0)//只获取未删除
            to = gd.GetThreads(m_fid,pageIndex,pageSize);
        else if(type==1)//获取全部
            to = gd.GetAllThreads(m_fid,pageIndex,pageSize);
        if (to == null)
            return;
        foreach (threads t in to)
        {
            Topic tt = new Topic(t);
            tt.CutTitle(TOPICKTITLELEN);
            topics.Add(tt);
        }
    }

    public void SearchAojProblem(int aid, int pageSize, int pageIndex, int type)
    {
        GetData gd = new GetData();
        List<threads> to = new List<threads>();
        if (type == 0)//只获取未删除
            to = gd.GetSearchAojProb(m_fid, aid, pageIndex, pageSize);
        else if (type == 1)//获取全部
            to = gd.GetSearchAllAojProb(m_fid, aid, pageIndex, pageSize);
        if (to == null)
            return;
        searchCount = to.Count();
        foreach (threads t in to)
        {
            Topic tt = new Topic(t);
            tt.CutTitle(TOPICKTITLELEN);
            topics.Add(tt);
        }
    }

    public void Publish()
    {
        try
        {
            this.m_fid = SaveData.NewForum(this);
        }
        catch (System.Exception ex)
        {
            //CGeneral.GoToErrorPage(Response, "custom", ex.ToString());
        }
    }

    public void Update()
    {
        try
        {
            SaveData.UpdateForum(this);
        }
        catch (System.Exception ex)
        {
            //出错处理
        }
    }

    public void UpdateAvatar(string originalPath)
    {
        string desPath = "";
        Image originalImage = Image.FromFile(originalPath);
        int ow = originalImage.Width;
        int oh = originalImage.Height;
        int toWidth = 0;
        int toHeight = 0;

        toHeight = toWidth = 50;
        desPath = System.IO.Path.GetDirectoryName(originalPath) + "\\" +"forum_"+ this.m_fid.ToString() + ".png";

        Image bitmap = new System.Drawing.Bitmap(toWidth, toHeight);//新建一个bmp图片
        Graphics g = System.Drawing.Graphics.FromImage(bitmap);//新建一个画板
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置高质量插值法
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度

        g.Clear(Color.White);//清空画布并以透明背景色填充
        g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(0, 0, ow, oh), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分
        try
        {
            if (System.IO.File.Exists(desPath))
                System.IO.File.Delete(desPath);
            bitmap.Save(desPath, System.Drawing.Imaging.ImageFormat.Png);

        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    public static bool isFidExist(int fid)
    {
        GetData gd = new GetData();
        if (gd.GetForum(fid) != null)
            return true;
        else
            return false;
    }
}